package cn.wyl.product.service.impl;

import cn.wyl.basic.service.IBaiduAiAuditService;
import cn.wyl.basic.util.AjaxResult;
import cn.wyl.basic.util.LoginContext;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.mapper.EmployeeMapper;
import cn.wyl.pet.domain.PetDetail;
import cn.wyl.product.domain.Product;
import cn.wyl.product.domain.ProductDetail;
import cn.wyl.product.domain.ProductOnlineAuditLog;
import cn.wyl.product.mapper.ProductDetailMapper;
import cn.wyl.product.mapper.ProductMapper;
import cn.wyl.product.mapper.ProductOnlineAuditLogMapper;
import cn.wyl.product.service.IProductService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        //获取当前登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            Product product = productMapper.findById(id);
            //1.百度AI智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            if(boo1 && boo2){
                //审核通过：修改服务状态state=1，上架时间 - 添加审核日志
                productMapper.updateOnsaleState(id,new Date());
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);//日志状态-通过
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核通过");
                productOnlineAuditLogMapper.save(log);
            }else {
                //审核失败：添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(0);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核失败");
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上线失败,服务的名称或图片违规");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(Long[] ids) {
        productMapper.updateOffsaleState(ids,new Date());
    }

    @Override
    public void save(Product product) {
        super.save(product);
        //服务详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void delete(Long id) {
        productDetailMapper.delDetailByProId(id);
        super.delete(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetailMapper.updateDetailByProId(product);
        }
    }

    @Override
    public void patchDelete(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }
}

